package com.microservice.skeleton.common.vo;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;

public class Main {

	
	public static void main(String[] args) {
		
		String userName = "webApp1";
		String userPass = "webApp2";
		byte[] tokenByte = Base64.encodeBase64((userName+":"+userPass).getBytes());
		
		String tokenStr = DataTypeChange.bytesSub2String(tokenByte, 0, tokenByte.length);

		System.out.println(tokenStr);
	}
	
	
	public static void getUserData(String url,String userName,String userPass){
		 //1.创建 HttpClient 的实例  
	try {
		BasicHttpParams httpParams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);
		HttpConnectionParams.setSoTimeout(httpParams, 20 * 1000);
		DefaultHttpClient client = new DefaultHttpClient(httpParams);
		HttpPost post = new HttpPost(url);
		post.setHeader("Content-Type", "application/json");
		//使用base64进行加密
		byte[] tokenByte = Base64.encodeBase64((userName+":"+userPass).getBytes());
		//将加密的信息转换为string
		String tokenStr = DataTypeChange.bytesSub2String(tokenByte, 0, tokenByte.length);
		//Basic YFUDIBGDJHFK78HFJDHF==    token的格式
		String token = "Basic "+tokenStr;
		//把认证信息发到header中
		post.setHeader("Authorization", "Basic "+token);
		HttpResponse response = client.execute(post);
		int statusCode = response.getStatusLine().getStatusCode();
		String retSrc = EntityUtils.toString(response.getEntity(), "utf-8");
		/*Log.i("Tag", "url=="+url);
		Log.i("Tag", "===statuscode==="+statusCode+"===retsrc==="+retSrc);*/
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (ClientProtocolException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}



		//return null;
	}

}
